<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 模板</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">
    <link href="${ctxStatic}/shopcartstatic/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="${ctxStatic}/shopcartstatic/css/font-awesome.min.css">
    <link rel="stylesheet" href="${ctxStatic}/shopcartstatic/css/aui.css">
    <link rel="stylesheet" href="${ctxStatic}/shopcartstatic/css/main.css">
    <link rel="stylesheet" href="${ctxStatic}/shopcartstatic/css/cart.css">
    <link rel="stylesheet" href="${ctxStatic}/shopcartstatic/css/theme-color.css">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->

</head>
<body>
<form method="post" name="cart_form" target="_self" id="cart_form" action="">
    <header class="zyw-header">
        <div class="zyw-container white-color">
            <div class="head-l"><a href="javascript:window.history.back(-1)" target="_self"><img
                    src="${ctxStatic}/shopcartstatic/images/head-return.svg" alt=""></a></div>
            <h1>购物车</h1>

        </div>
    </header>
    <footer class="zyw-footer">
        <div class="zyw-container white-bgcolor">
            <div class="settle_box">
                <dl class="all_check select">
                    <dt>
                        <span id="all_pitch_on"></span>
                        <em>全选</em>
                    </dt>
                </dl>
                <dl class="total_amount">
                    <dt>
                        合计：
                        <p id="total_price">
                            ¥<b>0</b>
                        </p>
                    </dt>
                    <dd>不含运费</dd>
                </dl>
                <input type="hidden" name="gcs" id="gcs"/>
                <a class="settle_btn" href="javascript:void(0);" id="confirm_cart">去结算</a>
                <a class="settle_btn" href="javascript:void(0);" id="confirm_cart1" onclick="big_cart_remove()">删除</a>
            </div>
        </div>
    </footer>
    <section class="zyw-container">
        <!--list-->
        <div class="commodity_list_box">
            <div class="cart_top">
                <span>商品清单</span>
                <div class="clear"></div>
            </div>

            <!--商品列表-->

            <c:forEach items="${list}" var="a">
                <div class="commodity_box">
                    <div class="commodity_list">
                        <!--店名信息-->
                        <div class="tite_tim select">
                            <em aem="1" cart_id="${a.product.id}" id="aaaaa"></em>
                            <span>${a.product.sellshop}</span>
                            <div class="clear"></div>
                        </div>

                        <!--商品-->
                        <ul class="commodity_list_term" ontouchstart="gtouchstart('${a.id}')"
                            ontouchmove="gtouchmove()" ontouchend="gtouchend()">

                            <li class="select">



                                <em aem="0" cart_id="${a.product.id}" class="goodsDetail"></em>

                                <img src="${a.product.imgList.get(0)}"/>
                                <input type="hidden" value="${a.product.id}" id="productId">

                                <div class="div_center">
                                    <h4>${a.product.name}</h4>
                                    <p class="now_value">
                                        <i>￥</i>
                                        <b class="qu_su">${a.product.price}</b>
                                    </p>
                                </div>

                                <div class="div_right">
                                    <i onclick="reducew(this)">-</i>
                                    <span class="zi">${a.num}</span>
                                    <input type="hidden" value="84">
                                    <i onclick="plusw(this)">+</i>
                                </div>

                            </li>
                        </ul>
                    </div>
                </div>
            </c:forEach>
            <!-- 商品列表 end -->
        </div>
        <!-- end -->
    </section>
</form>
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/shopcartstatic/js/ionic.bundle.min.js"></script>
<script type="text/javascript" src="${ctxStatic}/shopcartstatic/js/runend.js"></script>
<script src="${ctxStatic}/shopcartstatic/js/bootstrap.min.js"></script>
<script type="text/javascript">
    var timeOutEvent = 0;//定时器
    //开始按
    function gtouchstart(id) {
        var a = id;
        timeOutEvent = setTimeout("longPress(" + a + ")", 500);//这里设置定时器，定义长按500毫秒触发长按事件
        return false;
    };

    //手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
    function gtouchend() {
        clearTimeout(timeOutEvent);//清除定时器
        return false;
    };

    //如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
    function gtouchmove() {
        clearTimeout(timeOutEvent);//清除定时器
        timeOutEvent = 0;

    };

    //真正长按后应该执行的内容
    function longPress(a) {
        timeOutEvent = 0;
        //执行长按要执行的内容，如弹出菜单
        if (window.confirm("确认要删除该商品吗")) {
            window.location.href = "/shop/client/cartdel?id=" + a;
        }

    }
</script>
<script type="text/javascript">

    //定义全局变量
    var i = 0;

    //金额总和
    var money = 0;

    //计算合计价格
    var cart_money = new Object();

    //全部商品ID
    var cart_id = new Object();
    //备份商品ID，用于全选后去掉全选又再次全选
    var cart_id_copy = new Object();

</script>
<script>
    var noX = 0;
    /* 没选中时点击加减计算数量  */
    var allThis = $(".commodity_box .select em");
    /*底部全选*/
    var totalH;

    /* 减  */
    function reduceMod(e, totalH, mod, noX) {
        var tn = e.siblings().find(".qu_su").text();
        /* 当前选中商品  */
        var tn1 = e.siblings().find(".zi").text();
        /* 商品数量  */
        if (mod != 2) {
            var Total = parseFloat(totalH) - (tn * tn1);
            /* 总价格减该商品总数价格  */
            $("#total_price b").text(Total.toFixed(2));
        } else {
            /* 合计加单价-1 */
            var Total = parseFloat(totalH) - parseFloat(tn);
            /* 总价格减该商品总数价格  */
            $("#total_price b").text(Total.toFixed(2));
        }

    }
    ;/* 加  */
    function plusMod(e, totalH, mod) {
        var tn = e.siblings().find(".qu_su").text();
        /* 当前选中商品  */
        var tn1 = e.siblings().find(".zi").text();
        /* 商品数量  */
        if (mod != 2) {
            var Total = parseFloat(totalH) + (tn * tn1);
            /* 总价格加上该商品总数价格  */
            $("#total_price b").text(Total.toFixed(2));
        } else {
            /* 合计加单价+1 */
            var Total = parseFloat(totalH) + (parseFloat(tn) + (noX - 1));
            /* 总价格加上该商品总数价格  */
            $("#total_price b").text(Total.toFixed(2));
        }

    }
    ;/*全选该店商品价格 加*/
    function commodityPlusMod(e, totalH) {
        var qu = e.parents(".commodity_list").find(".pitch_on").parent().find(".qu_su");
        var quj = e.parents(".commodity_list").find(".pitch_on").parent().find(".zi");
        console.log(quj);
        var Total = 0;
        var erTotal = true;
        /* 该商品全部金额  */
        for (var i = 0; i < qu.length; i++) {
            var n = qu.eq(i).text();
            var n1 = quj.eq(i).text();
            /*合计价格*/
            if (erTotal) {
                Total = parseFloat(totalH) + (parseFloat(n) * parseFloat(n1));
                if (Total < 0)
                    Total = 0;
                erTotal = false;
            } else {
                Total = parseFloat(Total) + (parseFloat(n) * parseFloat(n1));

            }
        }
        $("#total_price b").text(Total.toFixed(2));
        /* 合计金额  */
    }
    ;var plus;

    /*全选该店商品价格 减*/
    function commodityReduceMod(e, totalH) {
        var qu = e.parents(".commodity_list").find(".pitch_on").parent().find(".qu_su");
        var quj = e.parents(".commodity_list").find(".pitch_on").parent().find(".zi");
        var Total = 0;
        plus = totalH;

        var erTotal = true;
        /* 该商品全部金额  */
        for (var i = 0; i < qu.length; i++) {
            var n = qu.eq(i).text();
            var n1 = quj.eq(i).text();
            /*合计价格*/
            if (erTotal) {
                Total = parseFloat(totalH) - (parseFloat(n) * parseFloat(n1));
                plus = Total;
                if (Total < 0)
                    Total = 0;
                erTotal = false;
            } else {
                Total = parseFloat(Total) - (parseFloat(n) * parseFloat(n1));
                plus = Total;

            }

            $("#total_price b").text(Total.toFixed(2));
            /* 合计金额  */
            plus;
        }
    }
    ;/*全部商品价格*/
    function commodityWhole() {
        /* 合计金额  */
        var je = $(".commodity_box .select .qu_su");
        /* 全部商品单价  */
        var je1 = $(".commodity_box .select .zi");
        /* 全部商品数量  */
        var TotalJe = 0;
        for (var i = 0; i < je.length; i++) {
            var n = je.eq(i).text();
            var n1 = je1.eq(i).text();
            TotalJe = TotalJe + (parseFloat(n) * parseFloat(n1));

        }
        $("#total_price b").text(TotalJe.toFixed(2));
        /* 合计金额  */
    }
    ;
    //选择结算商品

    //单选商品
    $(".select em").click(function () {
        var su = $(this).attr("aem");
        var carts_id = $(this).attr("cart_id");
        console.log("选中的商品id为" + carts_id);
        totalH = $("#total_price b").text();
        /* 合计金额  */
        if (su == 0) {
            /* 单选商品  */
            if ($(this).hasClass("pitch_on")) {
                /*去该店全选*/
                $(this).parents("ul").siblings(".select").find("em").removeClass("pitch_on");
                /*去底部全选*/
                $("#all_pitch_on").removeClass("pitch_on");
                $(this).removeClass("pitch_on");
                reduceMod($(this), totalH);
                cart_id[carts_id] = "";
                delete cart_id[carts_id];
            } else {
                $(this).addClass("pitch_on");
                var n = $(this).parents("ul").children().find(".pitch_on");
                var n1 = $(this).parents("ul").children();
                plusMod($(this), totalH, 0, noX);
                cart_id[carts_id] = "";
                /*该店商品全选中时*/
                if (n.length == n1.length) {
                    $(this).parents("ul").siblings(".select").find("em").addClass("pitch_on");
                }
                /*商品全部选中时*/
                var fot = $(".commodity_list_box .tite_tim .pitch_on");
                var fot1 = $(".commodity_list_box .tite_tim em");
                if (fot.length == fot1.length)
                    $("#all_pitch_on").addClass("pitch_on");
            }
        } else {
            /* 全选该店铺  */
            if ($(this).hasClass("pitch_on")) {
                /*去底部全选*/
                $("#all_pitch_on").removeClass("pitch_on");
                $(this).removeClass("pitch_on");

                commodityReduceMod($(this), totalH);
                $(this).parent().siblings("ul").find("em").removeClass("pitch_on");
                delete cart_id[carts_id];
            } else {
                commodityReduceMod($(this), totalH);

                $(this).addClass("pitch_on");

                $(this).parent().siblings("ul").find("em").addClass("pitch_on");

                /*if(plus != NaN && plus != undefined && plus > 0){
                 totalH = parseFloat(totalH)-parseFloat(plus);
                 if(totalH < 0)
                 totalH = 0;
                 }*/
                if (plus == undefined || plus == NaN) {
                    plus = 0
                }

                commodityPlusMod($(this), plus);
                cart_id[carts_id] = "";
                /*商品全部选中时*/
                var fot = $(".commodity_list_box .tite_tim .pitch_on");
                var fot1 = $(".commodity_list_box .tite_tim em");
                if (fot.length == fot1.length) {
                    $("#all_pitch_on").addClass("pitch_on");
                }

            }
        }

        //计算选择数值
        //number();

    });
    /* 底部全选  */

    var bot = 0;
    $("#all_pitch_on").click(function () {
        if (bot == 0) {
            $(this).addClass("pitch_on");
            allThis.removeClass("pitch_on");
            allThis.addClass("pitch_on");
            /*总价格*/
            commodityWhole();
            bot = 1;
            //重新加入属性对象
            for (var key in cart_id_copy) {
                cart_id[key] = "";
            }
        } else {
            $(this).removeClass("pitch_on");
            allThis.removeClass("pitch_on");
            $("#total_price b").text("0");
            bot = 0;
            //移除全部对象
            for (var key in cart_id) {
                delete cart_id[key];
            }
        }
        //计算选择数值
        //number();
    });

    function number() {
        var num = 0;
        for (var key in cart_id) {
            num++;
        }
        //将选择的放入到计算里面
        //$("#confirm_cart").html("结算("+num+")");
    }

    /* 编辑商品  */
    var topb = 0;
    $("#rem_s").click(function () {
        if (topb == 0) {
            $(this).text("完成");
            $(".total_amount").hide();
            /* 合计  */
            $("#confirm_cart").hide();
            /* 结算  */
            $("#confirm_cart1").show();
            /* 删除 */
            topb = 1;
        } else {
            topb = 0;
            $(this).text("编辑");
            $(".total_amount").show();
            /* 合计  */
            $("#confirm_cart").show();
            /* 结算  */
            $("#confirm_cart1").hide();
            /* 删除 */
            allThis.removeClass("pitch_on");
            /* 取消所有选择  */
            $("#all_pitch_on").removeClass("pitch_on");
            /* 取消所有选择  */
            $("#total_price b").text("0");
            /*合计价格清零*/

        }

    });

    /* 加减  */

    function reducew(obj) {
        //减
        var $this = $(obj);
        var totalH = $("#total_price b").text();
        /* 合计金额  */
        var ise = $this.siblings("span").text();
        var gc_id = $this.siblings("input").val();
        if (noX <= 0) {
            noX = 0;
        } else {
            noX--;
        }
        ;
        if (parseInt(ise) <= 1) {
            $this.siblings("span").text("1");
        } else {
            var n = parseInt(ise) - 1;
            $this.siblings("span").text(n);
            if ($this.parent().parent().children("em").hasClass("pitch_on")) {
                var mo = $this.parent().parent().children("em");
                reduceMod(mo, totalH, 2, noX);
                noX = 0;
            }

        }

        //goods_count_adjust(gc_id,n,null);
    }
    ;

    function plusw(obj) {
        //加
        var $this = $(obj);
        var totalH = $("#total_price b").text();
        /* 合计金额  */
        var ise = $this.siblings("span").text();
        var gc_id = $this.siblings("input").val();
        var n = parseInt(ise) + 1;
        noX++;

        $this.siblings("span").text(n);
        if ($this.parent().parent().children("em").hasClass("pitch_on")) {
            var mo = $this.parent().parent().children("em");
            plusMod(mo, totalH, 2, noX);
            noX = 0;
        }

        //goods_count_adjust(gc_id,n,null);
    }

    //删除
    function big_cart_remove() {
        $(".commodity_list_term .pitch_on").parent().remove();
        $(".commodity_list .tite_tim > em.pitch_on").parents(".commodity_box").remove();
    }
</script>


<script type="text/javascript">
    $("#confirm_cart").click(function () {

        //获取所有商品的对象
        var list = $(".goodsDetail.pitch_on");
        var jsonMap = "";
        for (var i = 0; i < list.length; i++) {
            var goodsId = $(list.get(i)).attr("cart_id");
            console.log(i + "---" + goodsId);
            var goodsNum = $(list.get(i).parentElement).find("span").html();//获取到该商品的数量
            console.log(i + "---" + goodsNum);
            //拼接map
            var map = "{goodsId: "+goodsId+", goodsNum:"+ goodsNum+"}";
            console.log(map);
            jsonMap+= map+",";
        }

        jsonMap = "["+jsonMap+"]";

        console.log(jsonMap);
        $.ajax({
            url: "jiesuan",
            data: {"mapList": jsonMap},
            success:function(data) {
                var n =1;
                $.each(data,function (i,a) {

                    if(n==1){
                        window.location.href="order-create?id="+a;
                        n++;
                    }

                })
                
                    
            }

        })


        // var  a = $(".pinth_on").siblings("#productId").val();
        //  var text = $(".pinth_on").sibling(".div_right").children("span").text();

    })
</script>
</body>
</html>